package com.example.zhou.musicplayer;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.os.EnvironmentCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private MediaPlayer mediaPlayer;
    private String musicName=new String();
    File musicFile;
    private TextView musicNameDisplay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mediaPlayer=new MediaPlayer();
        Button play=(Button)findViewById(R.id.play);
        Button pause=(Button)findViewById(R.id.pause);
        Button stop=(Button)findViewById(R.id.stop);
        Button list=(Button)findViewById(R.id.list);
        musicNameDisplay=(TextView)findViewById(R.id.musicNameDisplay);
        play.setOnClickListener(this);
        pause.setOnClickListener(this);
        stop.setOnClickListener(this);
        list.setOnClickListener(this);
        if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!=
                PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},0);
        }
        else{
            InitialzeMediaPlayer();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
        if(requestCode==0)
        {
            if(grantResults[0]==PackageManager.PERMISSION_GRANTED) {
                InitialzeMediaPlayer();
            }
            else{
                Toast.makeText(this,"Can't work without Permission",Toast.LENGTH_LONG).show();
                finish();
            }
        }
    }

    @Override
    public void onClick(View v){
        switch(v.getId())
        {
            case R.id.play:
                if(musicName.equals(""))
                {
                    Toast.makeText(this,"select a song from SD card to play!",Toast.LENGTH_LONG).show();
                }
                else if(!mediaPlayer.isPlaying()){
                    mediaPlayer.start();
                    musicNameDisplay.setText(musicName+"\nplaying");
                }
                break;
            case R.id.pause:
                if(mediaPlayer.isPlaying()) {
                    mediaPlayer.pause();
                    musicNameDisplay.setText(musicName+"\npaused");
                }
                break;
            case R.id.stop:
                if(mediaPlayer.isPlaying()){
                    mediaPlayer.reset();
                    InitialzeMediaPlayer();
                    musicNameDisplay.setText(musicName+"\nstoped");
                }
                break;
            case R.id.list:
                Intent intent=new Intent(MainActivity.this,SongList.class);
                startActivityForResult(intent,0);
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data)
    {
        if(requestCode==0)
        {
            musicName=data.getStringExtra("selectedMusicName");
            mediaPlayer.reset();
            InitialzeMediaPlayer();
            mediaPlayer.start();
            musicNameDisplay.setText(musicName+"\nplaying");
        }
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        if(mediaPlayer!=null){
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }

    private void InitialzeMediaPlayer(){
        try{
            if(!musicName.equals(""))
            {
                musicFile=new File(Environment.getExternalStorageDirectory(),musicName);
                mediaPlayer.setDataSource(musicFile.getPath());
                mediaPlayer.prepare();
            }
        }
        catch(IOException e){
            Log.e("MainActivity","file not exists");
        }
    }

}
